package ar.edu.unicen.pladema.ui.mywidgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

import ar.edu.unicen.pladema.main.SWTUtils;
import ar.edu.unicen.pladema.ui.panel.AbstractPanel;
import ar.edu.unicen.pladema.vc1.model.GraphicAttributes;

public class FileSelector extends AbstractPanel {
	private Text filedir;
	private Button selectFile;
	private GraphicAttributes graphicAttributes;
	
	public FileSelector(Composite parent) {
		super(parent);
	}

	@Override
	protected void createWidgets() {
		filedir = new Text(this, SWT.BORDER);
		selectFile = SWTUtils.createButton(this, "SELECT_FILE", new Listener() {
				@Override
				public void handleEvent(Event arg0) {
					selectFile();
				}
			});
	}
	private void selectFile() {
		FileDialog fd = new FileDialog(getShell());
        fd.setText("Abrir archivo");
        fd.setFileName(filedir.getText());
        String fileName = fd.open();
        if (fileName == null) {
          return;
        }
        
        if (graphicAttributes != null) {
        	System.out.println("ga noolo " + fileName);
        	graphicAttributes.add(GraphicAttributes.FILE_NAME, fileName);
        } else {
        	System.out.println("ga null " + fileName);
        	filedir.setText(fileName);
        }
	}
	
	@Override
	protected void layoutWidgets() {
		setLayout(new FormLayout());
		selectFile.setLayoutData(SWTUtils.createRightFormData(0));
		filedir.setLayoutData(SWTUtils.createFullLeftFormData(selectFile));
	}

	public void setFile(String fn) {
		if (fn == null) return;
		filedir.setText(fn);
	}
	public String getFile() {
		return filedir.getText();
	}

	public void setGraphicAttributes(GraphicAttributes graphicAttributes2) {
		this.graphicAttributes = graphicAttributes2;
		
	}
	
}
